/*!
 * 对话框相关操作
 */
(function($) {
	$.dk_lightbox = function(object, options) {
		
		options = $.extend({},options);
		var jqobj = $(object);
		var jqobjWidth = jqobj.outerWidth();
		var jqobjHeight = jqobj.outerHeight();
		var ie6		= ($.browser.msie && $.browser.version < 7);
		var bbody	= $(document.body);
		var bwindow	= $(window);
		var bdocument	= $(document);

		//if(ie6){
				$('select').css('visibility','hidden');
		//}
		
		if(options.opacity){
			if($('#dk_lightboxfade')){
			$('#dk_lightboxfade').remove();
			}
			bojbfade = $("<div class=\"dk_lightboxfade\" id=\"dk_lightboxfade\"></div>").appendTo(bbody);
		}

		var ie6scroll = function(){
			jqobj.css({top:bwindow.scrollTop()+(bwindow.height() - jqobj.outerHeight())/2});
		};
		
		var positionLightBox = function(){
			jqobj.css({
				position: (ie6) ? "absolute" : "fixed",
				top: (ie6) ? (bwindow.height() - jqobjHeight)/2+bwindow.scrollTop() : (bwindow.height() - jqobjHeight)/2,
				left: "50%",
				zIndex:"3280",
				marginLeft: (jqobjWidth/2)*-1
			});
			if(options.opacity){
				bojbfade.css({
					position: "absolute",
					zIndex: 3270,
					height: bbody.height(),
					width: "100%",
					background: (options.background) ? options.background : "#000000",
					opacity: options.opacity,
					top: 0,
					left: 0,
					right: 0,
					bottom: 0
				});
			}
		};

		var removePrompt = function(){
			if(options.opacity){bojbfade.hide();}
			//if($('#dk_lightboxfade').css('display')!='none'){
				$('#dk_lightboxfade').remove();
			//}
			jqobj.hide();
			if(ie6) {bbody.unbind('scroll',ie6scroll);}
			bwindow.unbind('resize',positionLightBox);
			jqobj.fadeOut('fast',function(){
				$('select').css('visibility','visible');
			});
			
		};
		
		positionLightBox();
		
		if(ie6) {
			bwindow.scroll(ie6scroll);
		}
		bwindow.resize(positionLightBox);
	
		if(options.closed){
			jqobj.find(options.closed).click(function(){removePrompt()});
		}
		
		if(options.closedtime){
			setTimeout(removePrompt,options.closedtime);
		}
		
		if(options.closedobj){
			$(options.closedobj).click(function(){removePrompt()});
		}
		jqobj.show();
	};
})($);

(function(window,undefined){
	
	var preventReClick = true; //设置防止重复点击全局变量

	window.hdialog = {
		//载入对话框
		load : function(url,callback,pos){	//第三个参数为“globalMiddle”时，dialog全局居中
			if(!preventReClick) return;
			preventReClick = false;
			var dlgPos = (pos == "globalMiddle") ? 0 : 40;
			$(".container").append('<div class="dialog-container"></div>');	
			$targetDialog = $(".dialog-container");
			$targetDialog.load(url,function(responseTxt,statusTxt,xhr){
				if(statusTxt=="success"){
					var w = $targetDialog.outerWidth(),
						h = $targetDialog.outerHeight() + dlgPos;
					$targetDialog.css({
						"margin-left":"-"+w/2+"px",
						"margin-top":"-"+h/2+"px"
					});
					hdialog.show(callback);
				}else if(statusTxt=="error"){
					alert("加载失败");
					$targetDialog.remove();
				}
			});		
		},
		//重载另一个页面
		reload: function(url,callback){
			hdialog.close(function(){
				hdialog.load(url,callback);
			});
		},
		//显示对话框
		show : function(callback){
			$(".overlay").fadeTo(300,0.3);
			$targetDialog.show(0,callback);
			preventReClick = true;
			$(".dialog-title .close").click(function(){
				hdialog.close();
			});
			$("#v_frame").hide();
		},
		//关闭对话框
		close : function(callback){
			$targetDialog.slideUp(300,function(){
				$targetDialog.remove();
				if(!!callback){callback()}
			});
			if(!callback){
				$(".overlay,.header-overlay").fadeOut(300);
				$("#v_frame").show();
			}
		}
	}

})(window);

//鼠标经过事件
function mouseEvent(obj){
	$(obj).mouseenter(function(){
		$(this).addClass("hover");
	}).mouseleave(function(){
		$(this).removeClass("hover");
	});
}
//初始化滚动条
function intiScroll(obj){
	$(obj).jscroll({
		W:"18px",
		BgUrl:"url(../../static/images/j-scroll-bg.png)",
		Bg:"left 0 repeat-y",
		Bar:{Bg:{Out:"-18px 0 repeat-y",Hover:"-28px 0 repeat-y",Focus:"-28px 0 repeat-y"}},
		Btn:{btn:false}
	});
}

//切换收藏Tabs
function changeTabs(a){
	_this = $("#collectTabs li").eq(a);
	_this.addClass("cur").siblings().removeClass("cur");
	$("#collectWrap li").eq(a).addClass("cur").siblings().removeClass("cur")
}
//弹出注册
function register(){
	hdialog.load("../../my/i-reg.html",function(){return},"globalMiddle");	
}
//转到注册页
function goRegister(){
	hdialog.reload("../../my/i-reg.html");	
}
//转到登录页
function goLogin(){
	hdialog.reload("../../my/i-login.html");	
}

/**
 * 全选|反选
 * @param	{String}	全选checkbox id
 * @param	{String}	子checkbox name
 */
function checkAll (id,name){
	$("#"+id+",input[name='"+name+"']").attr("checked",false);
	var $subBox = $("input[name='"+name+"']");
	var $targBox = $("#"+id);
	$targBox.click(function(){
		$subBox.attr("checked",this.checked);
	});
	$subBox.click(function(){
		$targBox.attr("checked",$subBox.length == $("input[name='"+name+"']:checked").length ? true : false);
	});
}

$(function(){
	
	/*******menu*********/
	$("#nav li").mouseenter(function(){
		var $thisLi = $(this);
		time = setTimeout(function(){
			$thisLi.find(".sub-nav").stop(true,true).slideDown(200);
		},200);
	}).mouseleave(function(){
		clearTimeout(time);
		$(this).find(".sub-nav").stop(true,true).slideUp(200);
	});
	/****************/
	
			
	$("#keyword").live("focus",function(){
		if($(this).val()==this.defaultValue){
			$(this).val("");
		}	
	}).live("blur",function(){
		if($(this).val()==""){
			$(this).val(this.defaultValue);
		}
	});
	
	//登录
	$("#login").bind("click",function(){
		hdialog.load("../../my/i-login.html");
	});
	//注册
	$("#register").bind("click",function(){
		hdialog.load("../../my/i-reg.html");
	});
	
	//收藏夹
	$("#collect").bind("click",function(){
		hdialog.load("../../my/i-collect.html",function(){
			mouseEvent(".acti-table tr");
		});
	}); 
	//名片盒
	$("#cardBox").bind("click",function(){
		hdialog.load("../../my/i-card.html",function(){
			mouseEvent(".acti-table tr");
			
		});
	});
	//在线用户
	$("#onlineUser").bind("click",function(){
		hdialog.load("../../my/i-online-user.html");
	});
	//个人信息
	$("#userInfo").bind("click",function(){
		hdialog.load("../../my/i-profile.html");
	});
	//资料下载
	$(".data-center").bind("click",function(){
		hdialog.load("../../my/i-data.html",function(){
			mouseEvent(".acti-table tr");		
		});
	});
	//展厅信息
	$(".hall-info").bind("click",function(){
		hdialog.load("../../my/i-info.html",function(){
			intiScroll("#infoScrollWrap")
		});
	});
})


